package com.pj.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pj.bean.BrandInfo;
import com.pj.respository.BrandInfoRepository;
import com.pj.service.ElasticSearchService;
import lombok.extern.slf4j.Slf4j;
import org.dromara.easyes.core.biz.EsPageInfo;
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/esSearch")
@Slf4j
public class ElasticSearchController {

    @Resource
    private ElasticSearchService elasticSearchService;

    @Resource
    BrandInfoRepository brandInfoRepository;

    @GetMapping("/getBrandAll")
    public String getBrandAll() throws JsonProcessingException {
        List<BrandInfo> brandInfos = elasticSearchService.getBrandAll("test");
        return new ObjectMapper().writeValueAsString(brandInfos);
    }


    @RequestMapping("/getBrandAllByPage")
    public String getBrandAllByPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) throws JsonProcessingException {
        EsPageInfo<BrandInfo> brandAllByPage = elasticSearchService.getBrandAllByPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("totalNum", brandAllByPage.getSize());
        map.put("totalPages", brandAllByPage.getPages());
        map.put("brandInfos", brandAllByPage.getList());
        return new ObjectMapper().writeValueAsString(map);
    }

    @GetMapping("/deleteBrandAll")
    public void getBrandAllByPage() {
        LambdaEsQueryWrapper<BrandInfo> wrapper = new LambdaEsQueryWrapper<>();
        brandInfoRepository.delete(wrapper);
    }

    @PostConstruct
    public void insert() {
        // 初始化-> 新增数据
        BrandInfo brandInfo = new BrandInfo();
        brandInfo.setId(IdUtil.getSnowflakeNextId());
        brandInfo.setBrandName(RandomUtil.randomNumbers(6));
        brandInfo.setAliasName(RandomUtil.randomString(10));
        brandInfo.setCreateTime(new Date());
        brandInfoRepository.insert(brandInfo);
        log.info("新增-brand_info---打印日志------{}", DateUtil.now());
    }

    @GetMapping("/search")
    public void search() {
        LambdaEsQueryWrapper<BrandInfo> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.eq(BrandInfo::getBrandName, "a")
                .and(i -> i.eq(BrandInfo::getId, 1).eq(BrandInfo::getEnglishName, "abc"));
        List<BrandInfo> brandInfos = brandInfoRepository.selectList(wrapper);

        log.info("brandInfos-----打印日志------{}",brandInfos.size() );
    }
}
